#include <QtTest>
#include "../tests_common.h"

#include "bitwin/roles.h"
#include "bitwin/rows.h"
#include "bitwin/columns.h"


using namespace BitWin;


Q_DECLARE_METATYPE(PRow)


class TestBitsColumn: public QObject
{
    Q_OBJECT

private slots:

    void testMisc_data();
    void testMisc();

    void testData_data();
    void testData();
};


void TestBitsColumn::testMisc_data()
{
    QTest::addColumn<QString>("title");
    QTest::addColumn<BitRange>("column_range");
    
    QTest::newRow("Case 1")
        << QString("Column 1") << BitRange(0, 8)
    ;
    QTest::newRow("Case 2")
        << QString("Column No. 2") << BitRange(1, 8)
    ;    
    QTest::newRow("Case 3")
        << QString("Column #3") << BitRange()
    ;
    QTest::newRow("Case 4")
        << QString("Column-4") << BitRange(3, 0)
    ;
}

void TestBitsColumn::testMisc()
{
    QFETCH(QString, title);
    QFETCH(BitRange, column_range);
    
    BitsColumn column(title, column_range);
    QCOMPARE(column.title(), title);
}

void TestBitsColumn::testData_data()
{
    QTest::addColumn<BitRange>("column_range");
    QTest::addColumn<PRow>("row");
    QTest::addColumn<QString>("indata");
    QTest::addColumn<QString>("outdata_expected");
    
    QTest::newRow("Column: [0, 8), whole word")
        << BitRange(0, 8)
        << PRow(new WordRow("Row", 0))
        << QString("abcdefgh")
        << QString("abcdefgh")
    ;
    QTest::newRow("Column: [1, 8), whole word")
        << BitRange(1, 8)
        << PRow(new WordRow("Row", 0))
        << QString("abcdefgh")
        << QString( "bcdefgh")
    ;
    QTest::newRow("Column: [3, 6), whole word")
        << BitRange(3, 6)
        << PRow(new WordRow("Row", 0))
        << QString("abcdefgh")
        << QString(   "def")
    ;
    QTest::newRow("Column: [3, 6), Field: [0, 8)")
        << BitRange(3, 6)
        << PRow(new FieldRow("Row", BitRange(0, 8), 0))
        << QString("abcdefgh")
        << QString(   "def")
    ;
    QTest::newRow("Column: [0, 8), Field: [3, 6)")
        << BitRange(0, 8)
        << PRow(new FieldRow("Row", BitRange(3, 6), 0))
        << QString("abcdefgh")
        << QString("   def  ")
    ;
    QTest::newRow("Column: [3, 8), Field: [3, 6)")
        << BitRange(3, 8)
        << PRow(new FieldRow("Row", BitRange(3, 6), 0))
        << QString("abcdefgh")
        << QString(   "def  ")
    ;
    QTest::newRow("Column: [3, 6), Field: [0, 8)")
        << BitRange(3, 6)
        << PRow(new FieldRow("Row", BitRange(0, 8), 0))
        << QString("abcdefgh")
        << QString(   "def")
    ;
    QTest::newRow("Column: [2, 6), Field: [4, 7)")
        << BitRange(2, 6)
        << PRow(new FieldRow("Row", BitRange(4, 7), 0))
        << QString("abcdefgh")
        << QString(  "  ef")
    ;
    QTest::newRow("Column: [4, 7), Field: [2, 6)")
        << BitRange(4, 7)
        << PRow(new FieldRow("Row", BitRange(2, 6), 0))
        << QString("abcdefgh")
        << QString(    "ef ")
    ;
}

void TestBitsColumn::testData()
{
    QString title("Column");
    QFETCH(BitRange, column_range);
    BitsColumn column(title, column_range);
    
    QFETCH(PRow, row);
    QFETCH(QString, indata);
    
    QVERIFY(
        !column
            .data(Qt::DisplayRole, row.data(), STRING_BITWISE(indata))
                .isValid()
    );
    QVERIFY(
        !column
            .data(BitwiseDataRole + 1, row.data(), STRING_BITWISE(indata))
                .isValid()
    );
    
    QVariant data(
        column.data(BitwiseDataRole, row.data(), STRING_BITWISE(indata))
    );
    QVERIFY(data.isValid());
    QVERIFY(data.canConvert<QString>());
    
    QString outdata(data.value<QString>());
    QFETCH(QString, outdata_expected);
    QCOMPARE(outdata, outdata_expected);
}


QTEST_MAIN(TestBitsColumn)


#include "main.moc"
